package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.common.globalException.LeaseException;
import com.atguigu.lease.common.result.ResultCodeEnum;
import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.BaseStatus;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.model.enums.LeaseStatus;
import com.atguigu.lease.web.admin.mapper.*;
import com.atguigu.lease.web.admin.service.LeaseAgreementService;
import com.atguigu.lease.web.admin.vo.agreement.AgreementQueryVo;
import com.atguigu.lease.web.admin.vo.agreement.AgreementVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author liubo
 * @description 针对表【lease_agreement(租约信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class LeaseAgreementServiceImpl extends ServiceImpl<LeaseAgreementMapper, LeaseAgreement>
        implements LeaseAgreementService {

    @Autowired
    private LeaseAgreementMapper leaseAgreementMapper;

    @Autowired
    private ApartmentInfoMapper apartmentInfoMapper;

    @Autowired
    private RoomInfoMapper roomInfoMapper;

    @Autowired
    private PaymentTypeMapper paymentTypeMapper;

    @Autowired
    private LeaseTermMapper leaseTermMapper;

    @Override
    // 分页查询租约列表
    public IPage<AgreementVo> pageAgreement(Page<AgreementVo> agreementPage, AgreementQueryVo queryVo) {
        return leaseAgreementMapper.pageAgreement(agreementPage, queryVo);
    }

    @Override
    // 根据id查询租约信息
    public AgreementVo selectById(Long id) {
        //1.先查询租约基本信息
        LeaseAgreement leaseAgreement = super.getById(id);

        if(leaseAgreement == null){
            throw new LeaseException(ResultCodeEnum.AMIND_LEASE_NOT_EXIST);
        }

        //2.根据id查询公寓基本信息
        LambdaQueryWrapper<ApartmentInfo> apartmentInfoWrapper = new LambdaQueryWrapper<>();
        apartmentInfoWrapper.eq(ApartmentInfo::getId, leaseAgreement.getApartmentId());
        ApartmentInfo apartmentInfo = apartmentInfoMapper.selectOne(apartmentInfoWrapper);

        //3.根据id查询房间基本信息
        LambdaQueryWrapper<RoomInfo> roomInfoWrapper = new LambdaQueryWrapper<>();
        roomInfoWrapper.eq(RoomInfo::getId, leaseAgreement.getRoomId());
        RoomInfo roomInfo = roomInfoMapper.selectOne(roomInfoWrapper);

        //4.根据id查询支付方式
        LambdaQueryWrapper<PaymentType> paymentTypeWrapper = new LambdaQueryWrapper<>();
        paymentTypeWrapper.eq(PaymentType::getId, leaseAgreement.getPaymentTypeId());
        PaymentType paymentType = paymentTypeMapper.selectOne(paymentTypeWrapper);

        //5.根据id查询租期
        LambdaQueryWrapper<LeaseTerm> leaseTermWrapper = new LambdaQueryWrapper<>();
        leaseTermWrapper.eq(LeaseTerm::getId, leaseAgreement.getLeaseTermId());
        LeaseTerm leaseTerm = leaseTermMapper.selectOne(leaseTermWrapper);

        //6.封装数据
        AgreementVo agreementVo = new AgreementVo();
        BeanUtils.copyProperties(leaseAgreement, agreementVo);
        agreementVo.setApartmentInfo(apartmentInfo);
        agreementVo.setRoomInfo(roomInfo);
        agreementVo.setPaymentType(paymentType);
        agreementVo.setLeaseTerm(leaseTerm);

        return agreementVo;
    }

    @Override
    //根据id修改租约状态
    public void updateStatusById(Long id, LeaseStatus status) {
        LambdaUpdateWrapper<LeaseAgreement> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(LeaseAgreement::getId, id);
        updateWrapper.set(LeaseAgreement::getStatus, status);
        super.update(updateWrapper);
    }
}




